package com.logic.landseaserver.service.impl;

import com.logic.common.exception.BusinessException;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.enums.BillStatusEnum;
import com.logic.landseaserver.common.enums.BillTypeEnum;
import com.logic.landseaserver.common.enums.StatisticsTypeEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.domain.Activity;
import com.logic.landseaserver.persistence.read.ActivityReadMapper;
import com.logic.landseaserver.persistence.read.BillReadMapper;
import com.logic.landseaserver.persistence.read.RoomReadMapper;
import com.logic.landseaserver.persistence.write.ActivityWriteMapper;
import com.logic.landseaserver.service.IActivityService;
import com.logic.landseaserver.service.IProjectAppartment;
import com.logic.landseaserver.service.IStatistic;
import com.logic.landseaserver.ws.dto.ActivityDTO;
import com.logic.landseaserver.ws.request.RoomStatusStatisticReq;
import com.logic.landseaserver.ws.response.IncomeStatisticResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: quintina
 * @Date: 2017/5/4
 * @Description: 活动接口实现类
 */
@Service
public class StatisticServiceImpl implements IStatistic {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IProjectAppartment projectAppartmentService;
    @Autowired
    private BillReadMapper billReadMapper;

    @Override
    public Map<String, Object> operateStatistic(RoomStatusStatisticReq req) throws LandseaBusinessException {

        Map<String, Object> roomStatistic = projectAppartmentService.roomStatistic(req);
        if(req.getType() != null && StatisticsTypeEnum.year.getCode().equals(req.getType().getCode())){
            req.setTime(DateUtil.getInstence().convertYear(new Date()));
        }
        if(req.getType() != null && StatisticsTypeEnum.month.getCode().equals(req.getType().getCode())){
            req.setTime(DateUtil.getInstence().convertYM(new Date()));
        }
        roomStatistic.put("allgrossed", billReadMapper.sumIncome(req));
        List<IncomeStatisticResp> list = billReadMapper.sumIncomeGroupByType(req);

        Double otherSum = new Double(0) ;
        for(IncomeStatisticResp resp: list){
            if (resp.getType() != null && BillTypeEnum.CTBI8.getCode().equals(resp.getType())) {
                roomStatistic.put("rentCharge", resp.getMoney());
            }else if(resp.getType() != null && BillTypeEnum.CTBI9.getCode().equals(resp.getType())){
                roomStatistic.put("serviceCharge", resp.getMoney());
            }else{
                otherSum += resp.getMoney();
            }
        }
        roomStatistic.put("otherCharge", otherSum);
        return roomStatistic;
    }
}
